/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package definitions;

import org.jsoup.select.Elements;
import org.pircbotx.hooks.*;
import org.pircbotx.hooks.events.*;
import org.pircbotx.hooks.types.*;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Date;


/**
 *
 * @author tangd
 */
public class CachingDefinitionFetcher extends DefinitionFetcher{
    private LinkedHashMap<String, Definition> defCache = new LinkedHashMap<String, Definition>();
    public CachingDefinitionFetcher(Connection c, String table) {
        super(c, table);
    }
    @Override
    public Definition getDefinition(String word) {
        String lowerCaseWord = word.toLowerCase();
        Definition def = defCache.get(lowerCaseWord);
        if(def==null || def.getDefinition()==null) {
            def = super.getDefinition(word);
            defCache.put(lowerCaseWord, def);
            return def;
        }
        return def;
    }
    @Override
    public Definition setDefinition(String word, String definition, String definer) {
        Definition ret = super.setDefinition(word, definition, definer);
        if(ret!=null) {
            defCache.put(ret.getWord(), ret);
        }
        return ret;
    }
    @Override
    public int removeDefinition(String word) {
        int ret = super.removeDefinition(word);
        if(ret!=0) {
            defCache.remove(word);
        }
        return ret;
    }
    
}
